package com.example.springcloudindex.web;

import com.example.springcloudindex.bean.User;
import com.example.springcloudindex.web.feigin.OrderAction;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
public class IndexAction {

    @RequestMapping("index")
    public String index(HttpServletRequest request){
        int port = request.getServerPort();
        return "index: " + port;
    }

    @Resource
    RestTemplate restTemplate;

    @HystrixCommand(fallbackMethod="hystrixOrder")
    @RequestMapping("order")
    public String order(){
        return restTemplate.getForObject(
                "http://spring-cloud-order/order",
                String.class);
    }

    public String hystrixOrder(){
        return "降级的order方法";
    }

    @RequestMapping("user")
    public String user(){
        return restTemplate.getForObject(
                "http://spring-cloud-user/user",
                String.class);
    }


    @Autowired
    OrderAction orderAction;

    @RequestMapping("orderForFeign")
    public String orderForFeign(){
        return orderAction.order();
    }

    @RequestMapping("sayHello1")
    public String sayHello1(String name){
        return orderAction.hello(name);
    }

    @RequestMapping("sayHello2")
    public String sayHello2(String name){
        return orderAction.helloA(name);
    }

    @RequestMapping("add/{x}/{y}")
    public String add(
            @PathVariable int x,
            @PathVariable int y){
        return "" + x + " + " + y + " = "
                + orderAction.add(x,y);
    }

    @RequestMapping("showme")
    public User showme(User user, String newName){
        return orderAction.showme(user, newName);
    }

}
